package cn.kbkwwk.os.controller;

import cn.kbkwwk.os.global.GlobalData;
import cn.kbkwwk.os.global.Mutex;
import cn.kbkwwk.os.thread.MyThreadCase1;
import cn.kbkwwk.os.thread.MyThreadCase2;
import cn.kbkwwk.os.thread.MyThreadCase3;
import cn.kbkwwk.os.thread.MyThreadCase4;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;


public class mainViewController {

    @FXML
    private RadioButton case1;

    @FXML
    private RadioButton case2;

    @FXML
    private RadioButton case3;

    @FXML
    private RadioButton case4;

    @FXML
    private ImageView forkImage0;

    @FXML
    private ImageView forkImage1;

    @FXML
    private ImageView forkImage2;

    @FXML
    private ImageView forkImage3;

    @FXML
    private ImageView forkImage4;

    @FXML
    private ImageView peopleImage0;

    @FXML
    private ImageView peopleImage1;

    @FXML
    private ImageView peopleImage2;

    @FXML
    private ImageView peopleImage3;

    @FXML
    private ImageView peopleImage4;

    @FXML
    private Button startExecute;

    @FXML
    private Button stopExecute;

    @FXML
    private ImageView tableImage0;

    @FXML
    private Font x1;

    @FXML
    private Color x2;

    @FXML
    private Font x3;

    @FXML
    private Color x4;


    @FXML
    private TextArea myText;

    public TextArea getMyText() {
        return myText;
    }

    public void setMyText(TextArea myText) {
        this.myText = myText;
    }
    /*主窗口的全局数据*/


    public RadioButton getCase1() {
        return case1;
    }

    public void setCase1(RadioButton case1) {
        this.case1 = case1;
    }

    public RadioButton getCase2() {
        return case2;
    }

    public void setCase2(RadioButton case2) {
        this.case2 = case2;
    }

    public RadioButton getCase3() {
        return case3;
    }

    public void setCase3(RadioButton case3) {
        this.case3 = case3;
    }

    public RadioButton getCase4() {
        return case4;
    }

    public void setCase4(RadioButton case4) {
        this.case4 = case4;
    }

    public ImageView getForkImage0() {
        return forkImage0;
    }

    public void setForkImage0(ImageView forkImage0) {
        this.forkImage0 = forkImage0;
    }

    public ImageView getForkImage1() {
        return forkImage1;
    }

    public void setForkImage1(ImageView forkImage1) {
        this.forkImage1 = forkImage1;
    }

    public ImageView getForkImage2() {
        return forkImage2;
    }

    public void setForkImage2(ImageView forkImage2) {
        this.forkImage2 = forkImage2;
    }

    public ImageView getForkImage3() {
        return forkImage3;
    }

    public void setForkImage3(ImageView forkImage3) {
        this.forkImage3 = forkImage3;
    }

    public ImageView getForkImage4() {
        return forkImage4;
    }

    public void setForkImage4(ImageView forkImage4) {
        this.forkImage4 = forkImage4;
    }

    public ImageView getPeopleImage0() {
        return peopleImage0;
    }

    public void setPeopleImage0(ImageView peopleImage0) {
        this.peopleImage0 = peopleImage0;
    }

    public ImageView getPeopleImage1() {
        return peopleImage1;
    }

    public void setPeopleImage1(ImageView peopleImage1) {
        this.peopleImage1 = peopleImage1;
    }

    public ImageView getPeopleImage2() {
        return peopleImage2;
    }

    public void setPeopleImage2(ImageView peopleImage2) {
        this.peopleImage2 = peopleImage2;
    }

    public ImageView getPeopleImage3() {
        return peopleImage3;
    }

    public void setPeopleImage3(ImageView peopleImage3) {
        this.peopleImage3 = peopleImage3;
    }

    public ImageView getPeopleImage4() {
        return peopleImage4;
    }

    public void setPeopleImage4(ImageView peopleImage4) {
        this.peopleImage4 = peopleImage4;
    }

    public Button getStartExecute() {
        return startExecute;
    }

    public void setStartExecute(Button startExecute) {
        this.startExecute = startExecute;
    }

    public Button getStopExecute() {
        return stopExecute;
    }

    public void setStopExecute(Button stopExecute) {
        this.stopExecute = stopExecute;
    }

    public ImageView getTableImage0() {
        return tableImage0;
    }

    public void setTableImage0(ImageView tableImage0) {
        this.tableImage0 = tableImage0;
    }


    @FXML
    void case1(ActionEvent event) {
        selectCase(getCase1());
    }

    @FXML
    void case2(ActionEvent event) {
        selectCase(getCase2());
    }

    @FXML
    void case3(ActionEvent event) {
        selectCase(getCase3());
    }

    @FXML
    void case4(ActionEvent event) {
        selectCase(getCase4());
    }

    /**
     * 停止执行按钮
     *
     * @param event
     */
    @FXML
    void stopExecute(ActionEvent event) {
        System.out.println("停止执行..");
        MyThreadCase1.setIsLoop(false);
        MyThreadCase2.setIsLoop(false);
        MyThreadCase3.setIsLoop(false);
        MyThreadCase4.setIsLoop(false);
    }

    /**
     * 开始执行
     *
     * @param event
     */
    @FXML
    void startExecute(ActionEvent event) {
//      初始化界面的文本和图片
        initStartExecute();
//      判断选择的case
        if (case1.isSelected()) {
            startCase1();
        } else if (case2.isSelected()) {
            startCase2();
        } else if (case3.isSelected()) {
            startCase3();
        } else {//默认执行4
            startCase4();
        }
    }

    //初始化文本和图片
    public void initStartExecute() {
        TextArea textArea = GlobalData.myController.getMyText();
        Platform.runLater(() -> {
            textArea.clear();
            forkImage0.setImage(GlobalData.forksImage[0]);
            forkImage1.setImage(GlobalData.forksImage[1]);
            forkImage2.setImage(GlobalData.forksImage[2]);
            forkImage3.setImage(GlobalData.forksImage[3]);
            forkImage4.setImage(GlobalData.forksImage[4]);
            peopleImage0.setImage(GlobalData.peopleImage);
            peopleImage1.setImage(GlobalData.peopleImage);
            peopleImage2.setImage(GlobalData.peopleImage);
            peopleImage3.setImage(GlobalData.peopleImage);
            peopleImage4.setImage(GlobalData.peopleImage);
        });
    }


    //    执行case1
    public void startCase1() {
        System.out.println("执行 仅允许左右两边有筷子才拿起");
        MyThreadCase1.setIsLoop(true);//设置进程可以循环
        MyThreadCase1.forks = new Integer[]{1, 1, 1, 1, 1};//初始化5个资源对应的信号量
        for (int i = 0; i < 5; i++) {
            new Thread(new MyThreadCase1(i)).start();
        }
    }

    //    执行case2
    public void startCase2() {
        System.out.println("执行 最多允许四个人同时就餐");
        Mutex mutex = new Mutex(4);
        MyThreadCase2.setIsLoop(true);//设置进程可以循环
        MyThreadCase2.forks = new Integer[]{1, 1, 1, 1, 1};//初始化5个资源对应的信号量
        for (int i = 0; i < 5; i++) {
            new Thread(new MyThreadCase2(mutex,i)).start();
        }
    }

    //    执行case3
    public void startCase3() {
        System.out.println("执行 奇数号拿左边，偶数号拿右边");
        MyThreadCase3.setIsLoop(true);//设置进程可以循环
        MyThreadCase3.forks = new Integer[]{1, 1, 1, 1, 1};//初始化5个资源对应的信号量
        for (int i = 0; i < 5; i++) {
            new Thread(new MyThreadCase3(i)).start();
        }
    }

    //    执行case4
    public void startCase4() {
        System.out.println("执行 可能出现死锁的情况");
        MyThreadCase4.setIsLoop(true);//设置进程可以循环
        MyThreadCase4.forks = new Integer[]{1, 1, 1, 1, 1};//初始化5个资源对应的信号量
        for (int i = 0; i < 5; i++) {
            new Thread(new MyThreadCase4(i)).start();
        }
    }


    //      选中单选框事件的激活效果
    public void selectCase(RadioButton id) {
//      初始化按钮的选中情况
        case1.setSelected(false);
        case2.setSelected(false);
        case3.setSelected(false);
        case4.setSelected(false);
//        激活指定的按钮
        id.setSelected(true);
    }

}
